package com.liuhung.base.util;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Date;

import org.apache.log4j.Logger;

import com.liuhung.base.entity.ImageCut;

import net.coobird.thumbnailator.Thumbnails;

public class ImageUtil {
	private final static Logger log = Logger.getLogger(ImageUtil.class);

	/**
	 * 获取图片宽度
	 * 
	 * @param file
	 *            图片文件
	 * @return 宽度
	 */
	public static int getImgWidth(File file) {
		InputStream is = null;
		BufferedImage src = null;
		int ret = -1;
		try {
			is = new FileInputStream(file);
			src = javax.imageio.ImageIO.read(is);
			ret = src.getWidth(null); // 得到源图宽
			is.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return ret;
	}

	public static int getImgWidthWithUrl(String url) {
		int ret = -1;
		if(ImageUtil.isConnect(url))
		{
			URL urls;
			try {
				urls = new URL(url);
				BufferedImage bi = null;
				try {
					bi = javax.imageio.ImageIO.read(urls);
				} catch (IOException e) {
					e.printStackTrace();
				}
				ret = bi.getWidth(null); // 得到源图宽
			} catch (MalformedURLException e) {
				e.printStackTrace();
			}
		}
		return ret;
	}

	/**
	 * 获取图片高度
	 * 
	 * @param file
	 *            图片文件
	 * @return 高度
	 */
	public static int getImgHeight(File file) {
		InputStream is = null;
		BufferedImage src = null;
		int ret = -1;
		try {
			is = new FileInputStream(file);
			src = javax.imageio.ImageIO.read(is);
			ret = src.getHeight(null); // 得到源图高
			is.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return ret;
	}

	public static String logoCut(ImageCut cut) {
		String oldPath = UrlUtil.getRealPath(cut.getImgUrl());
		String ext = oldPath.substring(oldPath.indexOf(".") + 1);
		String newPath = PropUtil.DEFAULT_UPLOAD_PATH + "\\cut\\";
		File targetDirFile = new File(newPath);
		if (!targetDirFile.exists()) {
			targetDirFile.mkdirs();
		}
		newPath = newPath + new Date().getTime() + "." + ext;
		String tempfile = PropUtil.DEFAULT_UPLOAD_PATH + "\\temp\\";
		File tempDirFile = new File(tempfile);
		if (!tempDirFile.exists()) {
			tempDirFile.mkdirs();
		}
		tempfile = tempfile + new Date().getTime() + "." + ext;
		try {
			Thumbnails.of(oldPath).size(cut.getNowWidth(), cut.getNowHeight())
					.toFile(tempfile);
			Thumbnails
					.of(tempfile)
					.sourceRegion(cut.getX(), cut.getY(), cut.getWidth(),
							cut.getHeight())
					.size(cut.getAutoWidth(), cut.getAutoHeight())
					.toFile(newPath);
			File tempFile2 = new File(tempfile);
			if (tempFile2.exists()) {
				tempFile2.delete();
			}
			return UrlUtil.getUrlPath(newPath);
		} catch (IOException e) {
			log.error("图片处理发生IO错误!");
			e.printStackTrace();
		}
		return null;
	}

	public static boolean isConnect(String url) {
		URL urlStr;
		HttpURLConnection connection;
		int state = -1;
		boolean succ = false;
		int counts = 0;
		while (counts < 5) {
			try {
				urlStr = new URL(url);
				connection = (HttpURLConnection) urlStr.openConnection();
				state = connection.getResponseCode();
				if (state == 200) {
					succ = true;
				}
				break;
			} catch (Exception ex) {
				counts++;
				continue;
			}
		}
		return succ;
	}

}
